

%% data, voting- and campaign-stage estimates, expected surpluses and winning probabilities (and gradients)

load('voting_stage.mat')
load('campaign_stage.mat')
load('coalition_stage_p1.mat')


%% partially-identified "entry" parameters

% (inverse) weighting matrix
G = [DES0(1:N0,:);DES1(N0+(1:N1),:);DES2(N0+N1+(1:N2),:)];
G = [mean(G-DES0,1);mean(G-DES1,1);mean(G-DES2,1)];
W = G * blkdiag(VgammaBeta,VB2S0) * G';

% no-candidate payoff parameters
[theta_NC_PVEM,theta_NC_PRI] = meshgrid(-2.8:0.001:-2.5,-3.3:0.001:-2.9); % fine grid search around identified set
q = zeros(size(theta_NC_PVEM)); exit_cfs = q;
optCFS = optimset('Display','off','HessFcn',@(gamma,lambda)HessQ_cfs(gamma,lambda,W));
parfor k = 1:numel(q)
    [~,q(k),exit_cfs(k)] = knitro_nlp(@(gamma)Q_cfs(gamma,ES0,theta_NC_PVEM(k)+ES1,theta_NC_PRI(k)+ES2,W,[N0,N1,N2]),...
        zeros(3,1),[],[],[],[],zeros(3,1),Inf(3,1),[],[],optCFS,'knitro.opt');
end
% "point estimates"
theta_NC_PVEM0 = theta_NC_PVEM(q == min(q(:)));
theta_NC_PRI0 = theta_NC_PRI(q == min(q(:)));
% identified set
theta_NC_PVEM = theta_NC_PVEM(NN * q <= chi2inv(0.95,3));
theta_NC_PRI = theta_NC_PRI(NN * q <= chi2inv(0.95,3));
plot(theta_NC_PVEM,theta_NC_PRI,'LineStyle','none','Marker','.','MarkerSize',10,'Color',[0.8,0.8,0.8])
hold on
plot(theta_NC_PVEM0,theta_NC_PRI0,'LineStyle','none','Marker','*','MarkerSize',15,'Color','k')
ax = gca;
title('No-Candidate Payoffs (95\% Confidence Set)','FontSize',16,'Interpreter','latex')
ax.YLabel.String = '$\theta_{\textnormal{PRI}}$';
ax.YLabel.FontSize = 14;
ax.YLabel.Interpreter = 'latex';
ax.YLabel.Rotation = 0;
ax.YLabel.VerticalAlignment = 'middle';
ax.YLabel.HorizontalAlignment = 'right';
ax.YLim = [-3.25,-2.95];
ax.YTick = -3.3:0.1:-2.9;
ax.XLabel.String = '$\theta_{\textnormal{PVEM}}$';
ax.XLabel.FontSize = 14;
ax.XLabel.Interpreter = 'latex';
ax.XLim = [-2.78,-2.48];
ax.XTick = -2.8:0.15:-2.4;
saveas(gcf,'../figures/figC1.png')
hold off


%%

clearvars -except G W theta_NC_PVEM theta_NC_PVEM0 theta_NC_PRI theta_NC_PRI0 q exit_cfs optCFS

save('coalition_stage_partial_id.mat')



